<template>
	<view>
		<uni-nav-bar :right-text="msg" :leftText="'筛选'" title="消息" @clickLeft="changeDate" @clickRight="changelist">
		</uni-nav-bar>
		<u-subsection :list="lists" :current="current" @change="sectionChange" v-if="usertype==2"></u-subsection>
		<view class="" v-if="usertype==2">
			<view class="" v-if="current==0">
				<u-swipe-action>
					<u-swipe-action-item :options="options" @click="del(index)" v-for="(item,index) in list"
						:key="index">
						<view class="p-2 border-bottom flex align-center">
							<view class="font30 font-weight-500 flex-1" :class="item.blRead=='0'?'color333':'color999'"
								style="height: 60rpx;" @click="listdetail(item)">
								{{item.content}}
							</view>
							<view class="" v-show="type==2" @click="clickCheck(index)">
								<image src="../../static/img/ic_unxuanzhong.png" style="width: 40rpx;height: 40rpx;"
									v-if="!item.flag">
								</image>
								<image src="../../static/img/ic_xuanzhong.png" style="width: 40rpx;height: 40rpx;"
									v-else>
								</image>
							</view>
						</view>
					</u-swipe-action-item>
				</u-swipe-action>
			</view>
			<view class="" v-else>
				<view class="p-2 border-bottom flex flex-wrap align-center" v-for="(item,index) in dslist" :key="index">
					<view class="font30 font-weight-500 flex-1 color333" style="" @click="detail(item)">
						您的孩子{{item.name}}于{{item.sendTime}}收到 {{item.course}} 的通知
					</view>
					<view class="" v-show="type==2" @click="clickdsCheck(index)">
						<image src="../../static/img/ic_unxuanzhong.png" style="width: 40rpx;height: 40rpx;"
							v-if="!item.flag">
						</image>
						<image src="../../static/img/ic_xuanzhong.png" style="width: 40rpx;height: 40rpx;" v-else>
						</image>
					</view>
				</view>
			</view>
		</view>
		<view class="" v-else>
			<view class="p-2 border-bottom flex flex-wrap align-center" v-for="(item,index) in jslist" :key="index">
				<view class="font30 font-weight-500 flex-1 color333" style="" @click="detail(item)">
					{{item.bjName}}的学生 {{item.student}} {{item.time}}的请假通知，时间 {{item.content}}
				</view>
				<view class="" v-show="type==2" @click="clickdsCheck(index)">
					<image src="../../static/img/ic_unxuanzhong.png" style="width: 40rpx;height: 40rpx;"
						v-if="!item.flag">
					</image>
					<image src="../../static/img/ic_xuanzhong.png" style="width: 40rpx;height: 40rpx;" v-else>
					</image>
				</view>
			</view>
		</view>
		<u-datetime-picker ref="datetimePicker" :show="show" v-model="sendDay" mode="date" @confirm="chioeDate"
			@cancel="closeData"></u-datetime-picker>
		<u-loadmore :status="status" />
	</view>
</template>

<script>
	let page = 1,
		timer = null;
	import uniNavBar from "../../components/uni-nav-bar/uni-nav-bar.vue"
	import moment from "moment"
	export default {
		components: {
			uniNavBar
		},
		data() {
			return {
				msg: '编辑',
				type: 1,
				list: [],
				dslist: [],
				jslist: [],
				status: 'loadmore',
				options: [{
					text: '删除',
					style: {
						backgroundColor: '#dd524d'
					}
				}],
				lists: ['消息', '代收通知'],
				current: 0,
				show: false,
				sendDay: moment().format('YYYY-MM-DD'),
				usertype: null,
			}
		},
		onShow() {
			this.usertype = uni.getStorageSync('intType');
			if (this.usertype == 2) {
				this.getData()
			} else {
				this.getjsList()
			}
		},
		methods: {
			changeDate() {
				this.show = true
			},
			closeData() {
				this.show = false
			},
			chioeDate(e) {
				this.sendDay = moment(e.value).format('YYYY-MM-DD');
				this.closeData()
				if (this.current == 1) {
					this.getdsList()
				}
			},
			sectionChange(index) {
				this.current = index;
				if (this.current == 1) {
					this.getdsList()
				}
			},
			getjsList() {
				let _this = this;
				let jsId = uni.getStorageSync('userInfo').user_id;
				let opts = {
					url: 'message/leaves?jsId=' + jsId,
				};
				let param = {
					jsId: jsId
				};
				_this.$http.httpDeveiceRequest(
					opts, param).then(
					res => {
						console.log(res.data);
						if (res.data.ReturnMsg == "success") {
							let arr = res.data.ReturnData;
							arr.map((item, i) => {
								item.flag = false;
							})
							_this.jslist = arr;
						} else {
							_this.jslist = [];
						}
					},
					error => {
						console.log(error);
					}
				);
			},
			getdsList() {
				let _this = this;
				let arr = uni.getStorageSync('userInfo').class_info;
				let xsId = arr[0].user_id;
				let opts = {
					url: 'message/homeworks?xsId=' +
						xsId + '&sendDay=' + _this.sendDay,
				};
				let param = {
					xsId: xsId,
					sendDay: _this.sendDay
				};
				_this.$http.httpDeveiceRequest(
					opts, param).then(
					res => {
						console.log(res.data);
						if (res.data.ReturnMsg == "success") {
							let arr = res.data.ReturnData;
							arr.map((item, i) => {
								item.flag = false;
							})
							_this.dslist = arr;
						} else {
							_this.dslist = [];
						}
					},
					error => {
						console.log(error);
					}
				);
			},
			del(index) {
				console.log(index)
				this.list.splice(index, 1)
			},
			listdetail(item) {
				uni.setStorageSync('msgdetail', item.content);
				let this_ = this;
				let opts = {
					url: 'UpdateMsg.shtml'
				};
				let param = {
					id: item.id
				};
				this_.$http.httpRequest(opts, param).then(
					res => {
						console.log(res.data);
						if (res.data.code == '200') {

						} else {
							uni.showToast({
								icon: 'none',
								title: res.data.msg
							});
						}
					},
					error => {
						console.log(error);
					}
				);
				uni.navigateTo({
					url: '../massagedetail/massagedetail'
				})
			},
			detail(item) {
				uni.setStorageSync('msgdetail', item.content)
				uni.navigateTo({
					url: '../massagedetail/massagedetail'
				})
			},
			changelist() {
				if (this.type == 1) {
					this.type = 2;
					this.msg = "删除"
				} else {
					if (this.current == 0) {
						let arr = this.list;
						let newArr = [];
						arr.map((item, i) => {
							if (!item.flag) {
								newArr.push(item)
							}
						})
						this.list = newArr;
					} else {
						let arr = this.dslist;
						let newArr = [];
						arr.map((item, i) => {
							if (!item.flag) {
								newArr.push(item)
							}
						})
						this.dslist = newArr;
					}
					this.type = 1;
					this.msg = "编辑"
				}
			},
			clickCheck(index) {
				let arr = this.list;
				if (arr[index].flag) {
					arr[index].flag = false
				} else {
					arr[index].flag = true
				}
				this.list = arr;
			},
			clickdsCheck(index) {
				let arr = this.dslist;
				if (arr[index].flag) {
					arr[index].flag = false
				} else {
					arr[index].flag = true
				}
				this.dslist = arr;
			},
			onPullDownRefresh() {
				//下拉的生命周期
				this.getData();
			},
			onReachBottom() {
				//阻止重复加载
				if (timer !== null) {
					clearTimeout(timer);
				}
				timer = setTimeout(() => this.getMoreData(), 500);
				// this.getMoreNews()
			},
			getData() {
				let this_ = this;
				let opts = {
					url: 'GetMsgPage.shtml'
				};
				let param = {
					page: page
				};
				this_.$http.httpTokenRequest(opts, param).then(
					res => {
						console.log(res.data);
						if (res.data.code == '200') {
							let arr = res.data.data;
							arr.map((item, i) => {
								item.flag = false;
							})
							this.list = arr;
							uni.stopPullDownRefresh();
							//隐藏标题读取
							uni.hideNavigationBarLoading();
							page++;
						} else {
							uni.showToast({
								icon: 'none',
								title: res.data.msg
							});
						}
					},
					error => {
						console.log(error);
					}
				);
			},
			getMoreData() {
				let this_ = this;
				uni.showNavigationBarLoading();
				this_.status = 'loading';
				let opts = {
					url: 'GetMsgPage.shtml'
				};
				let param = {
					page: page
				};
				this_.$http.httpTokenRequest(opts, param).then(
					res => {
						console.log(res.data);
						if (res.data.code == '200') {
							if (res.data.data == '') {
								this_.status = 'nomore';
								return;
							}
							this_.status = 'loadmore';
							this_.list = this_.list.concat(res.data.data);
							uni.stopPullDownRefresh();
							//隐藏标题读取
							uni.hideNavigationBarLoading();
							page++;
						} else {
							uni.showToast({
								icon: 'none',
								title: res.data.msg
							});
						}
					},
					error => {
						console.log(error);
					}
				);
			}
		}
	}
</script>

<style>

</style>
